package cn.vshare.controller;

import cn.vshare.entity.Comment;
import cn.vshare.entity.User;
import cn.vshare.service.CommentService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: 张恒
 * @Date: 2018/9/17 17:05
 * @Description:comment的controller层
 */
@RestController
public class CommentController {

    @Resource(name = "commentServiceImpl")
    private CommentService commentService;

    @RequestMapping("/getAllComment.do")
    public Map queryAllComment(String videoId, Integer pageNum, Integer pageSize) {
        Map map = new HashMap();
        String code = "";
        PageInfo<Comment> pageInfo = commentService.queryAllComment(videoId, pageNum, pageSize);
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            code = "0";
            map.put("pageInfo", pageInfo);
        } else {
            code = "1";
            map.put("msg", "获取评论列表失败!(╥╯^╰╥)");
        }
        map.put("code", code);
        return map;
    }

    @RequestMapping("/getCommentCount.do")
    public Integer queryCommentCount(String videoId) {
        return commentService.queryCommentCount(videoId);
    }


    @RequestMapping("/addComment.do")
    public Map addComment(HttpSession session, String videoId, String context, String date) {
        Map map = new HashMap();
        User currUser = (User) session.getAttribute("USER");
        if (!context.equals("")) {
            Comment newComment=commentService.addComment(currUser,videoId,context,date);
            if (newComment!=null) {
                map.put("newComment",newComment);
                map.put("code", 0);
                map.put("msg", "评论成功!ヾ(◍°∇°◍)ﾉﾞ");
            } else {
                map.put("code", 1);
                map.put("msg", "评论失败!o(╥﹏╥)o");
            }
        }
        return map;
    }


    @RequestMapping("/delComment.do")
    public Map delComment(String commentId) {
        Map map = new HashMap();
        if (commentId != null && !commentId.equals("")) {
            if (commentService.delComment(commentId)) {
                map.put("code", 0);
                map.put("msg", "删除成功!ヾ(◍°∇°◍)ﾉﾞ");
            } else {
                map.put("code", 1);
                map.put("msg", "删除失败!o(╥﹏╥)o");
            }
        }
        return map;
    }

    @RequestMapping("/updateCommentAddLike.do")
    public Map updateCommentAddLike(HttpSession session, String commentId){
        Map map =new HashMap();
        User user= (User) session.getAttribute("USER");
        try {
            if(commentService.updateCommentAddLike(user,commentId)){
                map.put("code",0);
                map.put("msg","点赞成功");
                Integer parseCount=commentService.queryCommentById(commentId).getParseCount();
                map.put("parseCount",parseCount);
            }else{
                map.put("code",1);
                map.put("msg","点赞失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("updateCommentCancelLike.do")
    public Map updateCommentCancelLike(HttpSession session, String commentId){
        Map map=new HashMap();
        User user= (User) session.getAttribute("USER");
        try {
            if(commentService.updateCommentCancelLike(user,commentId)){
                Integer parserCount=commentService.queryCommentById(commentId).getParseCount();
                map.put("code",0);
                map.put("msg","取消成功");
                map.put("parseCount",parserCount);
            }else{
                map.put("code",1);
                map.put("msg","取消失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping("/queryCommentIfLike.do")
    public String queryCommentIfLike(HttpSession session, String commentId){
        User user = (User) session.getAttribute("USER");
        if(commentService.queryCommentIfLike(commentId,user)==1){
            return "true";
        }
        return "";
    }

    /**
     * 获取评论
     *
     * @param condition
     * @return
     */
    @RequestMapping("/getComments")
    public Map<String, Object> getComments(String condition) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject cond = JSON.parseObject(condition);
        Page<Comment> commentPage = commentService.getCommentList(cond);
        map.put("totalRows", commentPage.getTotal());
        map.put("totalPages", commentPage.getPages());
        map.put("commentList", commentPage);
        map.put("currPage", cond.get("currPage"));
        return map;
    }

    @RequestMapping("/delCommentById")
    public String delCommentById(String id) {
        commentService.delCommentById(id); //删除评论
        return "success";
    }

    @RequestMapping("/forwardCommentList")
    public ModelAndView forwardCommentList() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("h-ui/comment-list");
        return mv;
    }
}
